﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using WareDealer;

namespace KillPrice
{
    public partial class UiDispatchArea : DevExpress.XtraEditors.XtraUserControl
    {
        public UiDispatchArea()
        {
            InitializeComponent();
        }

        Stopwatch _stopwathc = new Stopwatch();
        /// <summary>
        /// 线程启动状态
        /// </summary>
        private bool _isProcess = false;
        private void btnGet_Click(object sender, EventArgs e)
        {
            Thread getAreaThread = new Thread(GetAreaThread) { Name = "GetAreaThread",IsBackground = true};
            getAreaThread.Start();
            btnGet.Text = "采集中";
            btnGet.Enabled = false;
        }

        private void GetAreaThread()
        {
            JdListener.GetInstance().ProcessBarIDs = new List<string>() { "main" };
            JdListener.GetInstance().InitProcess = InitProcess;
            JdListener.GetInstance().ShowStep = ProcessStep;
            JdListener.GetInstance().EndProcess = EndProcess;
            JdListener.GetInstance().ShowMessage = ShowMessage;
            JdListener.GetInstance().GetWareDispatchAreas();
        }

        private void InitProcess(string processid, int leng)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string, int>(InitProcess), processid, leng);
            }
            else
            {
                {
                    //设置一个最小值
                    runProcessBar.Properties.Minimum = 0;
                    //设置一个最大值
                    runProcessBar.Properties.Maximum = leng;
                    //设置步长，即每次增加的数
                    runProcessBar.Properties.Step = 1;
                    //显示数字
                    runProcessBar.Properties.ShowTitle = true;
                    //当前位置
                    runProcessBar.Position = 0;
                    //设置进度条的样式
                    runProcessBar.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid;
                }

                _isProcess = leng > 0;
            }
        }

        private void ProcessStep(string processid, int iStep)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string, int>(ProcessStep), processid, iStep);
            }
            else
            {
                {
                    runProcessBar.Text = iStep.ToString();
                    runProcessBar.EditValue = iStep;
                }

            }
        }

        private void EndProcess(string processid, bool bEnd)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<string, bool>(EndProcess), "", bEnd);;
            }
            else
            {
                _isProcess = !bEnd;
                if (bEnd)
                {
                    _stopwathc.Stop();
                    btnGet.Text = "结束";
                    ShowMessage("采集商品地区信息结束！");
                }
            }
            
        }

        private void ShowMessage(string msg)
        {
            if (txtMsg.InvokeRequired)
            {
                Action<string> ac = new Action<string>(ShowMessage);
                txtMsg.Invoke(ac, msg);
            }
            else
            {
                txtMsg.Text = txtMsg.Text + (DateTime.Now.ToString() + " " + msg + "\r\n");
                txtMsg.SelectionStart = txtMsg.Text.Length;
                txtMsg.ScrollToCaret();
            }
        }
    }
}
